#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import subprocess


def sort(source_file, target_file):
    subprocess.Popen("sort -t '\t' -k3 -nr", shell=True,
                    stdin=open(source_file, 'r+'),
                    stdout=open(target_file, 'w'), 
                    )
    
def main():
    output_file_name = '/home/ivy/git/Social/Social/resource/temp.txt'
    sort_file_name = '/home/ivy/git/Social/Social/resource/follow.txt'
    
    current_user = ''
    follow_dict = {}
    with open(output_file_name, "a+") as outputfile:
        for i in range(1, 15):
            print i
            input_file_name = '/home/ivy/git/Social/Social/resource/json_tweets/json_tweets_%d.txt' % i
        
            with open(input_file_name, "r") as inputfile:
                for line in inputfile:
                    obj = json.loads(line)
                    user = obj['users']
                    status = obj['status']
                    if user != current_user:
                        #write into file
                        for key, value in follow_dict.iteritems():
                            outputfile.write('%s\t%s\n' % (key, value))
                        current_user = user
                        follow_dict.clear()
                    
                    if 0 == status:
                        continue
                    key = ''
                    if 1 == status:
                        key = '%s\t%s' % (user, obj['quoted_user'])
                    else:
                        key = '%s\t%s' % (user, obj['retweeted_user'])
                    if follow_dict.has_key(key):
                        value = follow_dict[key] + 1
                        follow_dict[key] = value
                    else:
                        follow_dict[key] = 1  
            
            outputfile.flush()
    
    outputfile.close()
    
    sort(output_file_name, sort_file_name)
    
if __name__ == '__main__':
    main() 